package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter05.function.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/*
reduce + process
聚合和窗口处理
 */
public class WindowProcessDemo2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env.socketTextStream("hadoop102",8888)
                .map(new WaterSensorMapFunction())
                .keyBy(WaterSensor::toString)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<WaterSensor>() {
                            @Override
                            public WaterSensor reduce(WaterSensor value1,
                                                      WaterSensor value2) throws Exception {
                                System.out.println("xxxx");
                                value1.setVc(value1.getVc() + value2.getVc());
                                return value1;
                            }
                        },
                        /*
                        性能更强一些
                        new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                            @Override
                            public void process(String s,
                                                Context ctx,
                                                //存储前面聚合的最终结果：有且仅有一个
                                                Iterable<WaterSensor> elements,
                                                Collector<String> out) throws Exception {
                                System.out.println("WindowProcessDemo2.process");
                                WaterSensor result = elements.iterator().next();
                                out.collect(ctx.window().getStart() + "    " + result);
                            }
                        }*/
                        new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                            @Override
                            public void apply(String s,
                                              TimeWindow timeWindow,
                                              Iterable<WaterSensor> elements,
                                              Collector<String> out) throws Exception {

                            }
                        }
                )

                .print();
             

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

